package com.znpc;

import com.znpc.entity.Order;
import com.znpc.valmap.ValMap;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * @Date 2021-09-05 15:50
 * @Author YangFan
 * @Description 获取数据
 */
public class DataReader {

    public static List<Order> getOrderList() throws IOException {
        //读取第一个工作表
        XSSFSheet sheet = getSheet0("/data/orders.xlsx");
        int totalRowNumber = sheet.getLastRowNum();

        //获取第一行的数据（标题）
        XSSFRow row;

        // 读取数据
        List<Order> orderList = new ArrayList();
        for (int i = 1; i <= totalRowNumber; i++) {
            row = sheet.getRow(i);
            Order order = new Order(row);
            orderList.add(order);
        }

        // 处理异常数据：同型号 不同基料/颜色/填料/填料比例，统一化
        for (Order o : orderList) {
            for (Order oo : orderList) {
                if (o.pName.equals(oo.pName)) {
                    if (o.orderId.equals(oo.orderId)) {
                        continue;
                    } else {
                        oo.color = oo.color.equals(o.color) ? oo.color : o.color;
                        oo.bm = oo.bm.equals(o.bm) ? oo.bm : o.bm;
                        oo.fm = oo.fm.equals(o.fm) ? oo.fm : o.fm;
                        oo.fmRation = oo.fmRation.equals(o.fmRation) ? oo.fmRation : o.fmRation;
                    }
                }
            }
        }

        return orderList;
    }

    public static Map<String, Map<Integer, Integer>> getCapacityMap() throws IOException {
        //读取第一个工作表
        XSSFSheet sheet = getSheet0("/data/capacity.xlsx");
        int totalRowNumber = sheet.getLastRowNum();

        //获取第一行的数据（标题）
        XSSFRow row;

        // 读取数据
        /*
         * {
         *   产品名称 : {
         *     产线 : 产能
         *   }
         * }
         */
        Map<String, Map<Integer, Integer>> capacityMap = new HashMap<>();
        for (int i = 1; i <= totalRowNumber; i++) {
            row = sheet.getRow(i);

            String pName = row.getCell(1).getStringCellValue();
            int lineId = (int) row.getCell(2).getNumericCellValue();
            int capacity = (int) row.getCell(3).getNumericCellValue();

            if (capacityMap.containsKey(pName)) {
                capacityMap.get(pName).put(lineId, capacity);
            } else {
                Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{
                    put(lineId, capacity);
                }};
                capacityMap.put(pName, map);
            }
        }
        return capacityMap;
    }

    public static Map<String, Map<String, Integer>> getBMScoreMap() throws IOException{
        //读取第一个工作表
        XSSFSheet sheet = getSheet0("/data/bmscore.xlsx");

        //获取第一行的数据（标题）
        XSSFRow row;
        List<String> bmList = Arrays.asList(ValMap.BASE_MATERIALS);

        /* 读取数据
         * {
         *   后序订单基料 : {
         *     前序订单基料 : 换产评分
         *   }
         * }
         */
        Map<String, Map<String, Integer>> bmScoreMap = new HashMap<>();
        for (int i = 1; i <=17 ; i++) {
            row = sheet.getRow(i);
            String latterBM = row.getCell(0).getStringCellValue();
            Map<String, Integer> preBMScoreMap = new HashMap<>();
            for (int j = 0; j < bmList.size(); j++) {
                int score = (int) row.getCell(j + 1).getNumericCellValue();
                preBMScoreMap.put(bmList.get(j), score);
            }
            bmScoreMap.put(latterBM, preBMScoreMap);
        }

        return bmScoreMap;
    }


    private static XSSFSheet getSheet0(String filePath) throws IOException {
        URL orderURL = MainApplication.class.getResource(filePath);
        File orderFile = new File(orderURL.getFile());
        XSSFWorkbook orderExcel = new XSSFWorkbook(new FileInputStream(orderFile));
        // 读取第一个工作表
        return orderExcel.getSheetAt(0);
    }
}
